package com.etone.smartAudit.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author: 蒋学伟
 * @date: 2018/1/3
 * @description: 分页器
 */
@Data
@ApiModel
@NoArgsConstructor
public class PagerVO<T> implements Serializable {

    private final static Integer DEFAULT_PAGE_SIZE = 10;
    @ApiModelProperty(notes = "每页显示数量", required = true)
    private Integer pageSize;
    @ApiModelProperty(notes = "总页数", required = true)
    private Long pageCount;
    @ApiModelProperty(notes = "当前页", required = true)
    private Integer page;
    @ApiModelProperty(notes = "总记录数", required = true)
    private Long total;
    @ApiModelProperty(notes = "是否有下一页", required = true)
    private Boolean hasNext;
    @ApiModelProperty(notes = "是否有上一页", required = true)
    private Boolean hasPre;
    @ApiModelProperty(notes = "数据列表", required = true)
    private List<T> rows;

    /**
     * @param page     当前页数
     * @param pageSize 每页显示多少
     * @param total    总记录数
     */
    public PagerVO(Integer page, Integer pageSize, Integer total) {
        this(page, pageSize, total.longValue());
    }


    public PagerVO(Integer page, Integer pageSize, Long total, List<T> rows) {
        this(page, pageSize, total);
        this.rows = rows;
    }

    public static PagerVO of(Integer page, Integer pageSize, Long total, List rows) {
        return new PagerVO(page, pageSize, total, rows);
    }

    /**
     * pagehelper中的 PageInfo 转 PagerVO
     *
     * @param pageInfo
     * @return
     */
    public static PagerVO of(PageInfo pageInfo) {
        return new PagerVO(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * pagehelper中的 Page 转 PagerVO
     *
     * @param page
     * @return
     */
    public static PagerVO of(Page page) {
        return new PagerVO(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getResult());
    }


    public static PagerVO of(IPage iPage) {
        return new PagerVO(Long.valueOf(iPage.getCurrent()).intValue(), Long.valueOf(iPage.getSize()).intValue(), iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 分页构造器
     *
     * @param pageSize 每页显示多少
     * @param page     当前页数
     * @param total    总记录数
     */
    public PagerVO(Integer page, Integer pageSize, Long total) {
        if (pageSize < 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }

        this.pageSize = pageSize;
        this.total = total;
        this.pageCount = (total + pageSize - 1) / pageSize;

        if (page <= 0) {
            page = 1;
        }
        if (pageCount == 0) {
            page = 1;
        } else {
            if (page != 1 && page > pageCount) {
                page = pageCount.intValue();
            }
        }
        //是否有上一页
        if (page <= 1) {
            this.hasPre = false;
        } else {
            this.hasPre = true;
        }
        //是否有下一页
        if (page.longValue() >= pageCount) {
            this.hasNext = false;
        } else {
            this.hasNext = true;
        }

        this.page = Integer.valueOf(page);
    }

}
